package com.dicti.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dicti.domain.Dicti;
import com.dicti.mapper.DictiMapper;
import com.dicti.service.DictiService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/24 15:14
 * @since JDK 1.8
 */
@Service
public class DictiServiceImpl extends ServiceImpl<DictiMapper, Dicti> implements DictiService {

    @Override
    public IPage<Dicti> dictiListPage(Integer page, Integer pageSize,String name) {
        LambdaQueryWrapper<Dicti> lambdaQueryWrapper=Wrappers.lambdaQuery();
        if(!StringUtils.isEmpty(name)){
            lambdaQueryWrapper.like(Dicti::getDictiName,name);
        }
        lambdaQueryWrapper.orderByDesc(Dicti::getCreateTime);
        return super.baseMapper.selectPage(new Page(page,pageSize),lambdaQueryWrapper);
    }

    @Override
    public Dicti selectDictiByName(String name) {
        LambdaQueryWrapper<Dicti> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Dicti::getDictiName,name);
        return super.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public int addDictiValueById(String id, String key, String value) {
        Dicti dicti=super.getById(id);
        if(dicti==null){
            return 0;
        }
        return addDictiCommon(dicti,key,value);
    }

    public int addDictiCommon(Dicti dicti,String key,String value){
        JSONArray jsonArray=null;
        if(dicti.getDictiValue()==null){
            jsonArray=new JSONArray();
        }
        if(dicti.getDictiValue()!=null){
            jsonArray= JSON.parseArray(dicti.getDictiValue());
            if(isHavekey(jsonArray,key)){
                return 0;
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key",key);
        jsonObject.put("value",value);
        jsonArray.add(jsonObject);
        dicti.setDictiValue(jsonArray.toJSONString());
        jsonArray.add(jsonObject);
        super.updateById(dicti);
        return 1;
    }



    private   boolean isHavekey(JSONArray jsonArray,String obj){
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject=(JSONObject) jsonArray.get(i);
            if(!jsonObject.containsKey("key")){
                continue;
            }
            String value=jsonObject.getString("key");
            if(value.equals(obj)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int addDictiValueByName(String name, String key, String value) {
        Dicti dicti=selectDictiByName(name);
        if(dicti==null){
            return 0;
        }
        return addDictiCommon(dicti,key,value);
    }

    @Override
    public int addDicti(String name) {
        Dicti dicti=selectDictiByName(name);
        if(dicti!=null){
            return 0;
        }
        Dicti dicti1=new Dicti();
        dicti1.setDictiName(name);
        JSONArray jsonArray=new JSONArray();
        dicti1.setDictiValue(jsonArray.toJSONString());
        super.baseMapper.insert(dicti1);
        return 1;
    }

    @Override
    public JSONArray getValuesById(String id) {
        Dicti dicti=super.getById(id);
        return getVlauesCommon(dicti);
    }

    public JSONArray getVlauesCommon(Dicti dicti){
        JSONArray jsonArray=new JSONArray();
        if(dicti==null){
            return jsonArray;
        }
        JSONArray jsonArray1=JSONObject.parseArray(dicti.getDictiValue());
        return jsonArray1;
    }

    @Override
    public JSONArray getValuesByName(String name) {
        Dicti dicti=selectDictiByName(name);
        return getVlauesCommon(dicti);
    }

    @Override
    public String getValueByName(String name, String key) {
        Dicti dicti=selectDictiByName(name);
        JSONArray jsonArray=getVlauesCommon(dicti);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject=(JSONObject)jsonArray.get(i);
            String  keyName=jsonObject.getString("key");
            if(keyName.equals(key)){
                String value=jsonObject.getString("value");
                return value;
            }
        }
        return "";
    }
}
